package org.shock.ss.sms.action.app;

import java.util.List;
import java.util.Map;

import org.shock.ss.sms.action.BaseAction;
import org.shock.ss.sms.bean.app.CargoInfo;
import org.shock.ss.sms.commons.annotation.RightAnno;
import org.shock.ss.sms.commons.dao.DaoFactory;
import org.shock.ss.sms.commons.utils.Util;
import org.shock.ss.sms.dao.app.impl.CargoDaoImpl;
import org.shock.ss.sms.service.app.CargoService;
import org.shock.ss.sms.service.app.impl.CargoServiceImpl;

import com.alibaba.fastjson.JSON;

public class CargoAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private CargoService cs = new CargoServiceImpl((CargoDaoImpl) DaoFactory.getDao("CargoDao"));
	private List cargoList;
	private CargoInfo cargo = new CargoInfo();
	private String ids;

	public String cargoAdmin() throws Exception {
		return SUCCESS;
	}

	public String listCargo() throws Exception {
		if (!Util.isEmpty(cargo.getCargo_name())) {
			sqladd.append(" and  cargo_name like '%" + cargo.getCargo_name() + "%'");
		}
		String sql = "select * from " + CargoInfo.getTable() + " where 1=1" + sqladd;
		String sqlCount = "select count(*) from " + CargoInfo.getTable() + " where 1=1" + sqladd;
		if (pageno == 1) {
			pageno = (start / rownum) + 1;
		}
		pager = cs.list(dir, sort, pageno, rownum, sqlCount, sql);
		totalCount = pager.getTotalCount();
		cargoList = pager.getList();
		jsonString = JSON.toJSONString(pager);
		System.out.println(jsonString);
		return SUCCESS;
	}

	public String viewCargo() throws Exception {
		Map map = cs.view("select * from " + cargo.getTable() + " where id=" + cargo.getId());
		jsonString = JSON.toJSONString(map);
		System.out.println(jsonString);
		return SUCCESS;
	}

	@RightAnno(rightValue = "delCargo", rightNote = "删除包类型")
	public String delCargo() throws Exception {
		cs.del("delete from " + cargo.getTable() + " where id in (" + ids + ")");
		success = true;
		message = "删除成功!";
		jsonString = "{success:" + success + ",message:'" + message + "'}";
		return SUCCESS;
	}

	@RightAnno(rightValue = "saveCargo", rightNote = "保存包类型")
	public String saveCargo() throws Exception {
		if (null == cargo.getId()) {
			return addCargo();
		} else {
			return updateCargo();
		}
	}

	public String updateCargo() throws Exception {
		cs.update(cargo);
		return SUCCESS;
	}

	public String addCargo() throws Exception {
		cs.add(cargo);
		return SUCCESS;
	}

	public List getCargoList() {
		return cargoList;
	}

	public void setCargoList(List cargoList) {
		this.cargoList = cargoList;
	}

	public CargoInfo getCargo() {
		return cargo;
	}

	public void setCargo(CargoInfo cargo) {
		this.cargo = cargo;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
